package com.liao.starapp;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.liao.starapp.utils.GetAssetsData;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private LinearLayout layout_fragment;
    private RadioButton main_rb_star;
    private RadioButton main_rb_partner;
    private RadioButton main_rb_luck;
    private RadioButton main_rb_user;
    private RadioGroup main_rg;

    private FragmentManager fm;
    private ArrayList<Fragment> fragments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        Window window = getWindow();
        int color = getColor(R.color.actionBarColor);
        window.setNavigationBarColor(color);
        window.setStatusBarColor(color);
        initView();
    }

    private void initView() {
        layout_fragment = (LinearLayout) findViewById(R.id.layout_fragment);
        main_rb_star = (RadioButton) findViewById(R.id.main_rb_star);
        main_rb_partner = (RadioButton) findViewById(R.id.main_rb_partner);
        main_rb_luck = (RadioButton) findViewById(R.id.main_rb_luck);
        main_rb_user = (RadioButton) findViewById(R.id.main_rb_user);
        main_rg = (RadioGroup) findViewById(R.id.main_rg);

        main_rb_star.setChecked(true);
        main_rg.setOnCheckedChangeListener(this);
        loadAssetsData();

        fm = getSupportFragmentManager();

        FragmentStarLayoutFragment sfs = new FragmentStarLayoutFragment();
        FragmentPartnerLayoutFragment fpf = new FragmentPartnerLayoutFragment();
        FragmentLuckLayoutFragment flf = new FragmentLuckLayoutFragment();
        FragmentUserLayoutFragment fuf = new FragmentUserLayoutFragment();
        fragments = new ArrayList<>();
        fragments.add(sfs);
        fragments.add(fpf);
        fragments.add(flf);
        fragments.add(fuf);
        addAllFragmentToFM();
        hideAllFragment();

        fm.beginTransaction().show(sfs).commit();
    }

    private void loadAssetsData() {
        GetAssetsData.saveAssetsImg(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.main_rb_star:
                showFragment(fragments.get(0));
                break;
            case R.id.main_rb_partner:
                showFragment(fragments.get(1));
                break;
            case R.id.main_rb_luck:
                showFragment(fragments.get(2));
                break;
            case R.id.main_rb_user:
                showFragment(fragments.get(3));
                break;
        }
    }

    private void showFragment(Fragment currFragment) {
        hideAllFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.show(currFragment);
        ft.commit();
    }

    private void hideAllFragment() {
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment fragment : fragments) {
            ft.hide(fragment);
        }
        ft.commit();
    }

    private void addAllFragmentToFM() {
        FragmentTransaction ft = fm.beginTransaction();
        for (Fragment fragment : fragments) {
            ft.add(R.id.layout_fragment, fragment);
        }
        ft.commit();
    }
}
